package cn.edu.neu.studentmsg_java.controller;

import cn.edu.neu.studentmsg_java.cons.R;
import cn.edu.neu.studentmsg_java.cons.State;
import cn.edu.neu.studentmsg_java.model.Admin;
import cn.edu.neu.studentmsg_java.service.LoginService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private LoginService loginService;

    //查询管理员账号密码
    @RequestMapping("/find")
    public R find(Admin admin){
        //通过用户名和密码查询是否存在该用户信息
        Admin a = loginService.findUserByNameAndPsd(admin);
        //判断结果是否为空
        if (ObjectUtils.isNotEmpty(a)) {
            return R.builder().code(State.SETTINGS_USER_LOGIN_SUCCESS.getCode()).msg(State.SETTINGS_USER_LOGIN_SUCCESS.getMsg()).build();
        }
        return R.builder().code(State.SETTINGS_USER_LOGIN_ACT_OR_LOGIN_PWD_ERROR.getCode()).msg(State.SETTINGS_USER_LOGIN_ACT_OR_LOGIN_PWD_ERROR.getMsg()).build();
    }

    //添加管理员账号密码
    @RequestMapping("/add")
    public R add(@RequestBody Admin admin) {
        //添加管理员信息
        int result = loginService.addAdmin(admin);
        //如果结果大于0，证明添加成功
        if (result > 0) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).build();
        }
        return R.builder().code(State.DB_SAVE_ERROR.getCode()).msg(State.DB_SAVE_ERROR.getMsg()).build();
    }
}
